<template>
    <view class="container">
        <header-bar :config="config"></header-bar>
        <view class="main">
            <area-select-page
                v-if="type == 0 && data && loadOk"
                :contentHeight="height"
                :address-data="data"
                @pickArea="pickArea"></area-select-page>
            <area-select-page-submit
                v-if="type == 1 && data && loadOk"
                :contentHeight="height"
                :address-data="data"
                @pickArea="pickArea"></area-select-page-submit>
        </view>
    </view>
</template>

<script>
import areaSelectPage from "@/uni-sub-base/components/area-select/area-select-page.vue"
import areaSelectPageSubmit from "@/uni-sub-base/components/area-select/area-select-page-submit.vue"
import { getElementHeight } from "@/uni-sub-base/utils/elementUtil.js"
export default {
    components: {
        areaSelectPage,
        areaSelectPageSubmit,
    },
    data() {
        return {
            config: {
                title: "选择地区",
                centerSlot: true,
            },
            data: null,
            height: "",
            loadOk: false,
            type: "0",
        }
    },
    mounted() {
        this.getListHeight()
    },
    onLoad(option) {
        if (option) {
            this.data = JSON.parse(decodeURIComponent(option.data))
            this.type = option.type
        }
    },
    methods: {
        getListHeight(element) {
            //获取商品高度
            getElementHeight(this, ".main", (e) => {
                this.height = e - uni.upx2px(120) + "px"
                this.loadOk = true
            })
        },
        //选择地区回调
        pickArea(data) {
            this.data = Object.assign(this.data, data)
            this.getRegionName()
            console.log(this.regionName)
            const eventChannel = this.getOpenerEventChannel()
            eventChannel.emit("getSelectData", {
                ...this.data,
                regionName: this.regionName,
            })
            this.easyGoBack()
        },
        getRegionName() {
            let line = "-"
            if (this.data.ProvinceId == 0) {
                this.regionName = this.data.all
            } else {
                this.regionName = `${this.data.ProvinceName || ""}${
                    this.data.CityName ? line + this.data.CityName : ""
                }${this.data.AreaName ? line + this.data.AreaName : ""}${
                    this.data.StreetName ? line + this.data.StreetName : ""
                }`
            }
        },
    },
}
</script>

<style lang="scss"></style>
